function Slider() {
	this.itemIndex = 0; //索引位置 必传
	this.sliderMain = document.querySelector(".slider-main");
	this.sliderContent = document.querySelector(".slider-content"); //
	this.sliderPage = document.querySelector(".slider-page"); //左右导航
	this.slidernav = null; //点点导航
	this.movewidth = this.sliderMain.offsetWidth; //移动的长度
	this.itemLength = this.sliderContent.querySelectorAll(".normal-item").length; //索引长度 
	this.openPage = true; //开左右导航
	this.openNav = true; //开点点导航
	this.sliderActive = true; //slider运行开关
	this.timer = null; //定时器
	this.autoplay = true; //默认自动轮播
	this.timeDelay = 3000; //设置轮播时间间隔
}
Slider.prototype.init = function(config) {
	if(config) {
		this.openPage = typeof config.openPage == "boolean" ? config.openPage : true;
		this.openNav = typeof config.openNav == "boolean" ? config.openNav : true;
		this.autoplay = typeof config.autoplay == "boolean" ? config.autoplay : true;
		this.timeDelay = typeof config.timeDelay == "number" ? config.timeDelay : 3000;
	}
	//设置容器的长度
	this.sliderContent.style.width = (this.itemLength + 2) * this.movewidth + "px";

	/*加入前后2个特殊的item*/
	var lastItem = this.sliderContent.querySelector(".normal-item").cloneNode(true);
	lastItem.className = "last-item";
	this.sliderContent.appendChild(lastItem);
	var firstItem = this.sliderContent.querySelectorAll(".normal-item")[this.itemLength - 1].cloneNode(true);
	firstItem.className = "first-item";
	this.sliderContent.insertBefore(firstItem, this.sliderContent.childNodes[0]);
	/*设置图片宽度为移动的宽度*/
	var _this=this;
	var allImg=this.sliderContent.querySelectorAll("img");
	for(var i=0;i<allImg.length;i++)
	{
		allImg[i].style.width=_this.movewidth+"px";
	}
	/*加入点点导航*/
	if(this.openNav) {
		var NavStr = '<ul class="slider-nav">'
		for(var i = 0; i < this.itemLength; i++) {
			if(i == 0)
				NavStr += '<li class="slider-item active" data-index="' + i + '"></li>';
			else
				NavStr += '<li class="slider-item" data-index="' + i + '"></li>';
		}
		NavStr += '</ul>';
		this.sliderMain.insertAdjacentHTML("afterEnd", NavStr);
		this.slidernav = document.querySelector(".slider-nav"); //点点导航
		//设置点点导航
		this.toIndexItem(this.itemIndex);
	}
	/*自动轮播*/
	if(this.autoplay)
		this.autoPlay();
	/*加如事件*/
	this.addEvent();
}
Slider.prototype.autoPlay = function() {
	var _this = this;
	this.timer = setInterval(function() {
		_this.toIndexItem(_this.itemIndex + 1);
	}, _this.timeDelay);
}
Slider.prototype.toIndexItem = function(num) {
	if(this.sliderActive) {
		this.sliderActive = false; //关掉开关
		var _this = this;
		setTimeout(function() {
			_this.sliderActive = true;
		}, 1000)
		num = parseInt(num);
		if(num < 0) {
			this.sliderContent.style.transition = "transform 1s ease-in-out";
			this.sliderContent.style.transform = "translate3d(0px, 0px, 0px)";
			this.itemIndex = this.itemLength - 1;
			setTimeout(function() {
				_this.sliderContent.style.transition = "none";
				_this.sliderContent.style.transform = "translate3d(-" + _this.movewidth * _this.itemLength + "px, 0px, 0px)";
			}, 1000)
		} else if(num >= this.itemLength) {
			this.sliderContent.style.transition = "transform 1s ease-in-out";
			this.sliderContent.style.transform = "translate3d(-" + this.movewidth * (this.itemLength + 1) + "px, 0px, 0px)";
			this.itemIndex = 0;
			setTimeout(function() {
				_this.sliderContent.style.transition = "none";
				_this.sliderContent.style.transform = "translate3d(-" + _this.movewidth + "px, 0px, 0px)";
			}, 1000)
		} else {
			this.sliderContent.style.transition = "transform 1s ease-in-out";
			this.sliderContent.style.transform = "translate3d(-" + this.movewidth * (num + 1) + "px, 0px, 0px)";
			this.itemIndex = num;
		}
		this.toIndexPoint(this.itemIndex);
	}
}
Slider.prototype.addEvent = function() {
	/*左右点击切换*/
	var _this = this;
	this.sliderPage.addEventListener("click", function(ev) {
		var ev = ev || window.event;
		var target = ev.target || ev.srcElement; //兼容ie
		if(target.className.indexOf("slider-pre") > -1)
			_this.toIndexItem(_this.itemIndex - 1)
		if(target.className.indexOf("slider-next") > -1)
			_this.toIndexItem(_this.itemIndex + 1)
	})
	/*点点切换*/
	if(this.openNav) {
		this.slidernav.addEventListener("click", function(ev) {
			var ev = ev || window.event;
			var target = ev.target || ev.srcElement; //兼容ie
			if(target.nodeName.toLowerCase() == "li") {
				if(target.className.indexOf("active") == -1) {
					_this.toIndexPoint(target.getAttribute("data-index"));
					_this.toIndexItem(target.getAttribute("data-index"));
				}
			}
		})
	}
	/*移入暂停 ，移除继续*/
	this.sliderMain.addEventListener("mouseover", function() {
		clearInterval(_this.timer);
	})
	this.sliderMain.addEventListener("mouseleave", function() {
		if(_this.autoplay)
			_this.autoPlay();
	})
}
Slider.prototype.toIndexPoint = function(num) {
	num = parseInt(num);
	if(this.openNav) {
		var target = this.slidernav.querySelectorAll(".slider-item")[num];
		var oldclass = target.parentNode.querySelector(".active").className;
		target.parentNode.querySelector(".active").className = oldclass.replace("active", "");
		target.className = target.className.replace(/\s$/, "") + " active";
	}
}

/*改变窗口大小*/
Slider.prototype.update =function(){
	this.movewidth = this.sliderMain.offsetWidth; //移动的长度
	/*设置图片宽度为移动的宽度*/
	var _this=this;
	var allImg=this.sliderContent.querySelectorAll("img");
	for(var i=0;i<allImg.length;i++)
	{
		allImg[i].style.width=_this.movewidth+"px";
	}
}
